﻿''' <summary>Řječnik setup wizard</summary>
Public Class frmFirstRun
    ''' <summary>Steps of wizard</summary>
    Private Steps As TabPage()

    ''' <summary>Allow close window</summary>
    Private AllowClose As Boolean = False

    Private Sub frmFirstRun_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Not AllowClose AndAlso e.CloseReason = CloseReason.None Then e.Cancel = True
    End Sub

    Private Sub frmFirstRun_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        AllowClose = True
        Me.Close()
    End Sub

    Private Sub cmdFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFinish.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
        AllowClose = True
        Me.Close()
    End Sub
    ''' <summary>CTor</summary>
    Public Sub New()
        InitializeComponent()
        rtbWellcome.Rtf = My.Resources.WizardTexts.Wellcome
        rtbDatabase.Rtf = My.Resources.WizardTexts.DBLocation
        rtbIco.Rtf = My.Resources.WizardTexts.ICOLocation
        rtbCompleted.Rtf = My.Resources.WizardTexts.Completed
        lblClickOnce.Text = If(My.Application.IsNetworkDeployed, My.Resources.IsClickOnce, My.Resources.IsNotClickOnce)
        lblClickOnce.Image = If(My.Application.IsNetworkDeployed, My.Resources.ClickOnce, My.Resources.NoClickOnce)
        lblIcoClickOnce.Text = lblClickOnce.Text
        lblIcoClickOnce.Image = lblClickOnce.Image
        lblĐŘječniksdf.Text = If(CType(StorageLocation(enmDatabaseLocations.ProgramFiles, ""), Tools.IOt.Path).IsFile, My.Resources.DatabaseFileProvided, My.Resources.NoDatabaseFileProvided)

        ReDim Steps(tabWiz.TabPages.Count - 1)
        For i As Integer = 0 To Steps.Length - 1
            Steps(i) = tabWiz.TabPages(i)
        Next i
        tabWiz.TabPages.Clear()
        CurrentStep = 0

        Select Case My.Settings.StorageLocation
            Case enmDatabaseLocations.CommonApplicationData : optLocShared.Checked = True
            Case enmDatabaseLocations.ProgramFiles : optLocProgramFiles.Checked = True
            Case enmDatabaseLocations.UserApllicationData : optLocPrivate.Checked = True
            Case enmDatabaseLocations.UserDefined : optLocUserDefined.Checked = True
        End Select
        If My.Application.IsNetworkDeployed Then
            optLocProgramFiles.Enabled = False
            optLocPrivate.Checked = True
            cmdDBProgramFiles.Enabled = False
        End If

        txtLocUserDefined.Text = My.Settings.StorageUserDefinedLocation
        Select Case My.Settings.IconLocation
            Case enmDatabaseLocations.CommonApplicationData : optIcoShared.Checked = True
            Case enmDatabaseLocations.ProgramFiles : optIcoProgramFiles.Checked = True
            Case enmDatabaseLocations.UserApllicationData : optIcoPrivate.Checked = True
            Case enmDatabaseLocations.UserDefined : optIcoUserDefined.Checked = True
        End Select
        If My.Application.IsNetworkDeployed Then
            optIcoProgramFiles.Enabled = False
            optIcoPrivate.Checked = True
            cmdIcoProgramFiles.Enabled = False
        End If
        txtIcoUserDefined.Text = My.Settings.IconUserDefinedLocation

        Dim ProgramFilesDBLocation As Tools.IOt.Path = StorageLocation(enmDatabaseLocations.ProgramFiles, "")
        cmdDBProgramFiles.Tag = ProgramFilesDBLocation - 1
        totHelp.SetToolTip(cmdDBProgramFiles, ProgramFilesDBLocation.Path & vbCrLf & My.Resources.ClickForBrowse)
        Dim AppDataDBLocation As Tools.IOt.Path = StorageLocation(enmDatabaseLocations.UserApllicationData, "")
        cmdDBUser.Tag = AppDataDBLocation - 1
        totHelp.SetToolTip(cmdDBUser, AppDataDBLocation.Path & vbCrLf & My.Resources.ClickForBrowse)
        Dim SharedDBLocation As Tools.IOt.Path = StorageLocation(enmDatabaseLocations.CommonApplicationData, "")
        cmdDBShared.Tag = SharedDBLocation - 1
        totHelp.SetToolTip(cmdDBShared, SharedDBLocation.Path & vbCrLf & My.Resources.ClickForBrowse)

        Dim ProgramFilesIcoLocation As Tools.IOt.Path = IconsLocation(enmDatabaseLocations.ProgramFiles, "")
        cmdIcoProgramFiles.Tag = ProgramFilesIcoLocation
        totHelp.SetToolTip(cmdIcoProgramFiles, ProgramFilesIcoLocation.Path & vbCrLf & My.Resources.ClickForBrowse)
        Dim AppDataIcoLocation As Tools.IOt.Path = IconsLocation(enmDatabaseLocations.UserApllicationData, "")
        cmdIcoUser.Tag = AppDataIcoLocation
        totHelp.SetToolTip(cmdIcoUser, AppDataIcoLocation.Path & vbCrLf & My.Resources.ClickForBrowse)
        Dim SharedIcoLocation As Tools.IOt.Path = IconsLocation(enmDatabaseLocations.CommonApplicationData, "")
        cmdIcoShared.Tag = SharedIcoLocation
        totHelp.SetToolTip(cmdIcoShared, SharedIcoLocation.Path & vbCrLf & My.Resources.ClickForBrowse)

    End Sub

    Private Sub cmdLocBw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLocBw.Click
        sfdLocBw.FileName = txtLocUserDefined.Text
        If sfdLocBw.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtLocUserDefined.Text = sfdLocBw.FileName
        End If
    End Sub

    Private Sub cmdPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrev.Click
        CurrentStep -= 1
    End Sub
    ''' <summary>Gets or sets index of current step of wizard</summary>
    Private Property CurrentStep() As Integer
        Get
            For i As Integer = 0 To Steps.Length - 1
                If tabWiz.SelectedTab Is Steps(i) Then Return i
            Next i
            Return -1
        End Get
        Set(ByVal value As Integer)
            If ChangeStep(CurrentStep, value) Then
                tabWiz.TabPages.Clear()
                tabWiz.TabPages.Add(Steps(value))
                StepChanged()
            End If
        End Set
    End Property
    ''' <summary>Steps of wizard</summary>
    Private Enum enmSteps
        ''' <summary>Wellcom step</summary>
        Wellcome = 0
        ''' <summary>Database location selector</summary>
        DatabaseLocation = 1
        ''' <summary>Icons location selector</summary>
        IconsLocation = 2
    End Enum

    ''' <summary>Tries to change step</summary>
    ''' <param name="current">Actual step</param>
    ''' <param name="next">Proposed new step</param>
    ''' <returns>True if step can be changed</returns>
    Private Function ChangeStep(ByVal current As enmSteps, ByVal [next] As enmSteps) As Boolean
        Select Case current
            Case enmSteps.DatabaseLocation 'Database location
                Dim loc As enmDatabaseLocations
                Select Case True
                    Case optLocPrivate.Checked : loc = enmDatabaseLocations.UserApllicationData
                    Case optLocProgramFiles.Checked : loc = enmDatabaseLocations.ProgramFiles
                    Case optLocShared.Checked : loc = enmDatabaseLocations.CommonApplicationData
                    Case optLocUserDefined.Checked : loc = enmDatabaseLocations.UserDefined
                    Case Else
                        MsgBox(My.Resources.YouMustSelectALocationOfDatabase, MsgBoxStyle.Exclamation, My.Resources.NoLocationSelected)
                        Return False
                End Select
                Dim DBLoc As String = StorageLocation(loc, txtLocUserDefined.Text)
                If IO.File.Exists(DBLoc) Then 'Try connection
                    If TestConnection(DBLoc) Then
                        My.Settings.StorageLocation = loc
                        My.Settings.StorageUserDefinedLocation = txtLocUserDefined.Text
                        Return True
                    Else
                        MsgBox(My.Resources.TestConnectionToSelectedDatabaseFileFailed, MsgBoxStyle.Critical, My.Resources.ConnectionError)
                        Return False
                    End If
                Else
                    Dim src As String
                    If Not My.Application.IsNetworkDeployed AndAlso IO.File.Exists(StorageLocation(enmDatabaseLocations.ProgramFiles, "")) Then 'Copy existing database file (empty or not) and try connection
                        src = StorageLocation(enmDatabaseLocations.ProgramFiles, "")
                    ElseIf My.Application.IsNetworkDeployed AndAlso IO.File.Exists(StorageLocation(enmDatabaseLocations.UserApllicationData, "")) Then 'ClickOnce default is another than non-ClickOnce
                        src = StorageLocation(enmDatabaseLocations.UserApllicationData, "")
                    Else
                        src = New Tools.IOt.Path(AppDomain.CurrentDomain.GetData(DataDirectory).ToString) + Emptysdf
                    End If
                    Try
                        IO.File.Copy(src, DBLoc)
                    Catch ex As Exception
                        MsgBox(My.Resources.ErrorWhileCopyingDatabaseFile & vbCrLf & ex.Message & vbCrLf & My.Resources.CheckDestinationPermision, MsgBoxStyle.Critical, My.Resources.Error_)
                        Return False
                    End Try
                    If TestConnection(DBLoc) Then
                        My.Settings.StorageLocation = loc
                        My.Settings.StorageUserDefinedLocation = txtLocUserDefined.Text
                        Return True
                    Else
                        MsgBox(My.Resources.TestConnectionToSelectedDatabaseFileFailed, MsgBoxStyle.Critical, My.Resources.ConnectionError)
                        Return False
                    End If
                End If
            Case enmSteps.IconsLocation 'Icons location selector
                Dim loc As enmDatabaseLocations
                Select Case True
                    Case optIcoPrivate.Checked : loc = enmDatabaseLocations.UserApllicationData
                    Case optIcoProgramFiles.Checked : loc = enmDatabaseLocations.ProgramFiles
                    Case optIcoShared.Checked : loc = enmDatabaseLocations.CommonApplicationData
                    Case optIcoUserDefined.Checked : loc = enmDatabaseLocations.UserDefined
                    Case Else
                        MsgBox(My.Resources.YouMustSelectALocationOfDatabase, MsgBoxStyle.Exclamation, My.Resources.NoLocationSelected)
                        Return False
                End Select
                Dim ICOLoc As String = IconsLocation(loc, txtIcoUserDefined.Text)
                If Not IO.Directory.Exists(ICOLoc) Then
                    Try
                        IO.Directory.CreateDirectory(ICOLoc)
                    Catch ex As Exception
                        MsgBox(My.Resources.FailedToCreateIconsDirectory & vbCrLf & ex.Message, MsgBoxStyle.Critical, My.Resources.Error_)
                        Return False
                    End Try
                End If
                Dim IcoSource As String
                If My.Application.IsNetworkDeployed Then
                    IcoSource = IO.Directory.Exists(IconsLocation(enmDatabaseLocations.UserApllicationData, ""))
                Else
                    IcoSource = IO.Directory.Exists(IconsLocation(enmDatabaseLocations.ProgramFiles, ""))
                End If
                If IcoSource AndAlso loc <> If(My.Application.IsNetworkDeployed, enmDatabaseLocations.UserApllicationData, enmDatabaseLocations.ProgramFiles) Then
                    Try
                        If Not CType(IcoSource, Tools.IOt.Path).Copy(ICOLoc, True) Then
                            If MsgBox(My.Resources.ThereWereSomeErrorDuringCopyingIcons, MsgBoxStyle.YesNo Or MsgBoxStyle.Exclamation, My.Resources.CopyIcons) = MsgBoxResult.No Then Return False
                        End If
                    Catch ex As Exception
                        If MsgBox(My.Resources.ThereWasAnErrorDuringCopyingIcons & vbCrLf & ex.Message & vbCrLf & vbCrLf & "Do you want to ignore it?", MsgBoxStyle.Exclamation Or MsgBoxStyle.YesNo, My.Resources.CopyIcons) = MsgBoxResult.No Then Return False
                    End Try
                End If
                My.Settings.IconLocation = loc
                My.Settings.IconUserDefinedLocation = txtIcoUserDefined.Text
                Return True
            Case Else : Return True
        End Select
    End Function

    Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
        CurrentStep += 1
    End Sub

    Private Sub optLocUserDefined_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optLocUserDefined.CheckedChanged
        txtLocUserDefined.Enabled = optLocUserDefined.Checked
        cmdLocBw.Enabled = optLocUserDefined.Checked
    End Sub
    ''' <summary>Handles changes of current step of wizard</summary>
    Private Sub StepChanged()
        Dim CS As Integer = CurrentStep
        cmdPrev.Enabled = CS > 0
        cmdNext.Enabled = CS < Steps.Length - 1
        cmdFinish.Enabled = CS = Steps.Length - 1
    End Sub

    Private Sub cmdPath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdIcoUser.Click, cmdIcoShared.Click, cmdIcoProgramFiles.Click, cmdDBUser.Click, cmdDBShared.Click, cmdDBProgramFiles.Click
        Dim TC As String = Nothing
        Try
            TC = IO.Path.Combine(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Ghisler\Total Commander", "InstallDir", Nothing), "TOTALCMD.EXE")
        Catch
            Try
                TC = IO.Path.Combine(My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\Ghisler\Total Commander", "InstallDir", Nothing), "TOTALCMD.EXE")
            Catch : End Try
        End Try
        Try
            Process.Start(TC, """" & DirectCast(DirectCast(sender, Button).Tag, Tools.IOt.Path).Path & """")
        Catch
            Try
                Process.Start(DirectCast(DirectCast(sender, Button).Tag, Tools.IOt.Path).Path)
            Catch : End Try
        End Try
    End Sub

    Private Sub cmdIcoBw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdIcoBw.Click
        fbdIcons.SelectedPath = txtIcoUserDefined.Text
        If fbdIcons.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtIcoUserDefined.Text = fbdIcons.SelectedPath
        End If
    End Sub

    Private Sub optIcoUserDefined_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optIcoUserDefined.CheckedChanged
        txtIcoUserDefined.Enabled = optIcoUserDefined.Checked
        cmdIcoBw.Enabled = optIcoUserDefined.Checked
    End Sub


End Class